package com.guxingke.gsb;

import com.guxingke.gsb.config.Config;
import com.guxingke.gsb.config.Constant;
import com.guxingke.gsb.config.ThemeConfig;
import com.guxingke.gsb.service.GsbService;
import com.guxingke.gsb.ui.cli.*;
import com.guxingke.gsb.util.PathUtil;
import org.kohsuke.args4j.*;
import org.kohsuke.args4j.spi.BooleanOptionHandler;
import org.kohsuke.args4j.spi.SubCommand;
import org.kohsuke.args4j.spi.SubCommandHandler;
import org.kohsuke.args4j.spi.SubCommands;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Created by 孤星可 on 2015/11/24.
 *
 * @author guxingke
 * @since 2015/11/24
 */
public class Gsb {
    private GsbService gsbService;
    private static Gsb me = new Gsb();

    @Option(name = "--version", aliases = {
            "-v" }, handler = BooleanOptionHandler.class, usage = "print the current version")
    boolean version = false;

    @Option(name = "--help", aliases = {
            "-h" }, handler = BooleanOptionHandler.class, usage = "print help message for the command")
    boolean help = false;

    @Argument(usage = "Execute sub commands", metaVar = "<commands>", handler = SubCommandHandler.class)
    @SubCommands({ @SubCommand(name = "init", impl = InitCommand.class),
            @SubCommand(name = "publish", impl = PublishCommand.class),
            @SubCommand(name = "preview", impl = PreviewCommand.class),
            @SubCommand(name = "clean", impl = CleanCommand.class)
    })
    public GsbCommand commands;

    private Gsb() {
        gsbService = new GsbService();
    }

    public static Gsb getInstance() {
        return me;
    }

    public static void main(String[] args) {
        Gsb gsb = Gsb.getInstance();

        CmdLineParser parser = new CmdLineParser(gsb, ParserProperties.defaults().withUsageWidth(120));
        try {
            parser.parseArgument(args);
        } catch (CmdLineException e) {
            e.printStackTrace();
        }

        if (gsb.help || gsb.version || args.length == 0) {
            gsb.printHelpMessage();
            parser.printUsage(System.out);
        } else {
            gsb.commands.execute();
        }
    }

    public boolean gsbInit(Path path) {
        return gsbService.init(path.toString());
    }

    public boolean publish() {
        Config.init(PathUtil.getClassRootPath());
        return gsbService.publish(Config.getSiteConfigPath());
    }

    private void printHelpMessage() {
        System.out.println("will be print help");
    }


    public void preview(Integer port) {
        Config.init(PathUtil.getClassRootPath());
        String siteConfigPath = Config.getSiteConfigPath();
        Path path = Paths.get(siteConfigPath, Constant.OUTPUT_DIRECTORY);
        gsbService.previewOnLocal(path.toString(),port);
    }

    public void previewStop(Integer port){
        gsbService.previewStop(port);
    }

    public void clean(boolean removeGitDir) {
        Config.init(PathUtil.getClassRootPath());
        gsbService.clean(Config.getSiteConfigPath(), removeGitDir);
    }
}
